package tk.winshu.argumentresolver.service;

import org.springframework.stereotype.Service;
import tk.winshu.argumentresolver.model.User;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Kevin
 * @since 2019/5/31
 */
@Service
public class UserServiceImpl implements IUserService {

    private static Map<String, User> caches = new ConcurrentHashMap<>();

    @Override
    public User getUser(String jsessionid) {
        return caches.get(jsessionid);
    }

    @Override
    public boolean login(String jsessionid, String code, String password) {
        User user = caches.get(jsessionid);
        if (user == null) {
            caches.put(jsessionid, new User(code, code));
            return true;
        }
        return Objects.equals(user.getPassword(), password);
    }

    @Override
    public void logout(String jsessionid) {
        caches.remove(jsessionid);
    }
}
